<?php

namespace app\common\model;

use think\Model;

/**
 * Imapp
 */
class Imapp extends Model
{
    // 表名
    protected $name = 'imapp';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $append = [
        'show_secret',
    ];

    public function admin(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\Admin::class, 'admin_id', 'id');
    }

    public static function onBeforeInsert($model): void
    {
        $appid = \ba\Random::build('noZero',16);
        $find = self::where('app_id', $appid)->find();
        while ($find) {
            $appid = \ba\Random::build('noZero',16);
            $find = self::where('app_id', $appid)->find();
        }
        $model->app_id = $appid;
        $model->app_secret = \ba\Random::build('sha1');
    }
    public function getShowSecretAttr($value): bool
    {
        return false;
    }
}